package org.example.system.controller.exam;

import jakarta.validation.Valid;
import org.example.common.core.controller.BaseController;
import org.example.common.core.domain.Result;
import org.example.common.core.domain.ResultTable;
import org.example.system.domain.exam.dto.ExamAddDTO;
import org.example.system.domain.exam.dto.ExamEditDTO;
import org.example.system.domain.exam.dto.ExamQueryDTO;
import org.example.system.domain.exam.dto.ExamQuestionAddDTO;
import org.example.system.domain.exam.vo.ExamDetailVO;
import org.example.system.service.exam.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private ExamService examService;

    @GetMapping("/list")
    public ResultTable list(ExamQueryDTO examQueryDTO) {
        return getTable(examService.list(examQueryDTO));
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody @Valid ExamAddDTO examAddDTO) {
        return Result.ok(examService.add(examAddDTO));
    }

    @PostMapping("/question/add")
    public Result<Void> questionAdd(@RequestBody ExamQuestionAddDTO examQuestionAddDTO) {
        return toResult(examService.questionAdd(examQuestionAddDTO));
    }

    @DeleteMapping("/question/delete")
    public Result<Void> questionDelete(Long examId, Long questionId) {
        return toResult(examService.questionDelete(examId, questionId));
    }

    @GetMapping("/detail")
    public Result<ExamDetailVO> detail(Long examId) {
        return Result.ok(examService.detail(examId));
    }

    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody @Valid ExamEditDTO examEditDTO) {
        return toResult(examService.edit(examEditDTO));
    }

    @DeleteMapping("/delete")
    public Result<Void> delete(Long examId) {
        return toResult(examService.delete(examId));
    }

    @PutMapping("/publish")
    public Result<Void> publish(Long examId) {
        return toResult(examService.publish(examId));
    }

    @PutMapping("/cancelPublish")
    public Result<Void> cancelPublish(Long examId) {
        return toResult(examService.cancelPublish(examId));
    }
}
